/**
 * 
 */
package com.xth8013.as.util;

/**
 * 
 * @Description:
 * @author: tianhua.xie
 * @Date: 2017年4月8日 下午12:00:18 
 * 
 */
public class BaseQueryHelper {

	public BaseQueryHelper() {
		super();
		pageNo = 1;
		pageSize = 10;
		orderType = "desc";

	}

	// ========对应easyUI的分页、排序查询的字段==============

	/** 当前页号，对应 pageNo */
	protected Integer page;
	/** 每页记录条数，对应 pageSize */
	protected Integer rows;
	/** 排序的字段，对应 orderField */
	protected String sort;
	/** 排序类型，对应 orderType */
	protected String order;

	// 只需要setter方法
	public void setPage(Integer page) {
		this.page = page;
		this.pageNo = page;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
		this.pageSize = rows;
	}

	public void setSort(String sort) {
		if ("".equals(sort)) {
			this.sort = null;
			this.orderField = null;
		} else {
			this.sort = sort;
			this.orderField = sort;
		}
	}

	public void setOrder(String order) {
		if ("".equals(sort)) {
			this.order = null;
			this.orderType = null;
		} else {
			this.order = order;
			this.orderType = order;
		}

	}

	// ===============================================

	/** 当前页号 */
	protected Integer pageNo;

	/** 每页记录条数 */
	protected Integer pageSize;
	/** 总共有多少条记录 */
	protected Integer totalRecNum;
	/** 记录开始位置 */
	protected Integer startIndex;
	/** 记录结束位置 */
	protected Integer endIndex;
	/** 记录结束位置 */
	protected Integer realyRecNum;

	/** 排序的字段 */
	protected String orderField;
	/** 排序类型 */
	protected String orderType;

	/**
	 * 总共有多少条记录
	 * 
	 * @return
	 */
	public Integer getTotalRecNum() {
		return totalRecNum;
	}

	/**
	 * 总共有多少条记录
	 * 
	 * @param totalRecNum
	 */
	public void setTotalRecNum(Integer totalRecNum) {
		// 总记录数应该比前pageNo - 1的记录数来得大
		if (totalRecNum <= pageSize * (pageNo - 1)) {
			this.setPage(1);
		}

		this.totalRecNum = totalRecNum;
	}

	public int getStartIndex() {
		return pageSize * (pageNo - 1);
	}

	public int getEndIndex() {
		int idx = (pageSize * pageNo > this.totalRecNum) ? this.totalRecNum : (pageSize * pageNo);
		return idx - 1;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public String getOrderField() {
		return orderField;
	}

	public String getOrderType() {
		return orderType;
	}

	public Integer getRealyRecNum() {
		return getEndIndex() - getStartIndex() + 1;
	}

	// ---------------预留setter----------------------
	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public void setOrderField(String orderField) {
		if ("".equals(orderField)) {
			this.orderField = null;
		} else {
			this.orderField = orderField;
		}
	}

	public void setOrderType(String orderType) {
		if ("".equals(orderType)) {
			this.orderType = null;
		} else {
			this.orderType = orderType;
		}

	}

	// ----------------------------------------------

	@Override
	public String toString() {
		return "BaseQueryHelper [pageNo=" + pageNo + ", pageSize=" + pageSize + ", totalRecNum=" + totalRecNum
				+ ", startIndex=" + startIndex + ", endIndex=" + endIndex + ", orderField=" + orderField
				+ ", orderType=" + orderType + "]";
	}


}
